package com.share.PACManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.share.PACManager.data.DatabaseHelper;
import com.share.PACManager.data.LocalService;
import com.share.PACManager.model.CodeModel;
import com.share.PACManager.model.ConfigModel;
import com.share.PACManager.model.TerminalModel;
import com.share.PACManager.model.TransactionModel;
import com.share.PACManager.utils.Utils;

public class Global
{
	public DatabaseHelper mDatabaseHelper;
	public TerminalModel mTerminalModel;	
	public ConfigModel mConfigModel;
	public TransactionModel mTransactionModel;
	public LocalService mBoundService; 
	
	public List<CodeModel> mCodeArea;
	public List<CodeModel> mCodeCardType;
	public List<CodeModel> mCodeCheckReason;
	public List<CodeModel> mCodeCompanyType;
	public List<CodeModel> mCodePersonStatus;
	public List<CodeModel> mCodeCompanyRegType;
	public List<CodeModel> mCodeNation;
	public List<CodeModel> mCodeNative;
	public List<CodeModel> mCodeSex;
	public List<CodeModel> mCodeTerminalStatus;
	public List<CodeModel> mCodeTerminalType;
	public List<CodeModel> mCodeWorkingStatus;
	
	public Global(Context context)
	{
		mDatabaseHelper = getDatabaseHelper(context);
		mTerminalModel = new TerminalModel();
		mConfigModel = new ConfigModel();
		mTransactionModel = new TransactionModel();		
		loadCodes();
	}
	
	private DatabaseHelper getDatabaseHelper(Context context)
	{
		checkPathExist(Utils.getDataPath());
		checkPathExist(Utils.getDataPath("data"));
		checkPathExist(Utils.getDataPath("images"));
		checkPathExist(Utils.getDataPath("temp"));
		int version = 10;		
		return new DatabaseHelper(context, Utils.getDataPath("data") + "/cache.db", version);
	}
	
	private void checkPathExist(String path)
	{
		File file = new File(path);
		if(!file.exists())
		{
			file.mkdirs();
		}
		else if(!file.isDirectory())
		{
			file.delete();
			file.mkdirs();
		}
	}
	
	public void loadCodes()
	{
		mCodeArea = getCodeList("Code_Area");
		mCodeCardType = getCodeList("Code_CardType");
		mCodeCheckReason = getCodeList("Code_CheckReason");
		mCodeCompanyType = getCodeList("Code_CompanyType");
		mCodePersonStatus = getCodeList("Code_PersonStatus");
		mCodeCompanyRegType  = getComppanyRegTypeList("Code_CompanyType");
		mCodeNation = getCodeList("Code_Nation");
		mCodeNative = getCodeList("Code_Native");
		mCodeSex = getCodeList("Code_Sex");
		mCodeTerminalStatus = getCodeList("Code_TerminalStatus");
		mCodeTerminalType = getCodeList("Code_TerminalType");
		mCodeWorkingStatus = getCodeList("Code_WorkingStatus");
	}
	
	private List<CodeModel> getComppanyRegTypeList(String CodeName)
	{
		List<CodeModel> codes = getCodeList(CodeName);
		for(int i = codes.size() - 1; i>=0; i--)
		{
			CodeModel code = codes.get(i);
			if(code.Name.equals("Free"))
				codes.remove(i);
		}
		return codes;
	}
	
	private List<CodeModel> getCodeList(String CodeName)
	{
		List<CodeModel> codes = new ArrayList<CodeModel>();
		mDatabaseHelper.queryCodes(codes, CodeName);
		return codes;
	}
}
